<?php

class AuthController extends Zend_Controller_Action
{
	/**
     * @var Zend_Translate
     */
    protected $_translate;
    
    /**
     * This will initialize CSS and javascript for the template system
     * Also gets the translator from application registry
     * 
     * @return void
     */
    public function init()
    {
        $this->_translate = Zend_Registry::get('Zend_Translate');
    }
    
    /**
     * Redirect to FireFeed Homepage
     * 
     * @return void
     */
    public function indexAction()
    {
    	$this->_redirect('/index');
    }
    
    /**
     * Show login form to the user
     * 
     * @return void
     */
    public function activeloginAction() 
    {
        $this->_helper->layout->setLayout('lightlayout');
     
        $this->view->formLogin = new Form_Login();
    }
    
    /**
     * Pagina di login dell'amministratore
     * 
     * @return void
     */
    public function ffadminAction()
    {
        //clean headScript e headLink
        $this->view->headScript()->exchangeArray(array());
        $this->view->headLink()->exchangeArray(array());
        
        $this->view->headLink()->appendStylesheet('/css/style.css');
        
        $this->_helper->layout->setLayout('ffadminlayout');
        $this->view->formLoginffadmin = new Form_Loginffadmin();
    }
    
    /**
     * Process user autentication
     * 
     * @return void
     */
    public function loginAction()
    {   
        $this->_helper->layout->setLayout('lightlayout');        
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $form = new Form_Login();
            $post = $request->getPost();

            if ($form->isValid($post)) {
                $values = $form->getValues();
                
                $serviceFactory = Model_Factory::getInstance();
                $service = $serviceFactory->getAuthService();
                
                $authResult = $service->authenticate(
                    $values['openid_identifier'], 
                    $values['password']
                );
                if (!$authResult) { // Authentication error
                    $this->view->errorMsg = 
                        $this->_translate->translate('authError'); 
                    
                    $this->view->formLogin = $form;
                    return $this->render('activelogin');
                } else { // Authentication OK
                	$user = Zend_Auth::getInstance()->getIdentity();
                    $this->_redirect($user->getRoles()->homepage);
                }
            } else { // Validation failed
                $this->view->formLogin = $form;
                return $this->render('activelogin');
            }
        } else { // Request type is not in POST so forward to index page
            $this->_forward('index');
        }
    }
    
    /**
     * Process OpenID autentication
     * 
     * @return void
     */
    public function loginopenidAction()
    {
        $this->_helper->layout->setLayout('lightlayout');
        
        $request = $this->getRequest();

        $form = new Form_Login();
        
        if ($request->isPost()) {
            $post = $request->getPost();

            if (!$form->isValid($post)) { // Validazione fallita
                $this->view->formLogin = $form;
                return $this->render('activelogin');
            }
        } 
        
        $openid_action = $request->get('openid_action');
        $openid_id = $request->get('openid_identifier');
        $openid_mode = $request->get('openid_mode');
        $openid_returnTo = 'loginopenid';
        $openid_identity = $request->get('openid_identity');
        
        if (($openid_action == "login" &&  !empty($openid_id)) ||
             $openid_mode != null) {
            $serviceFactory = Model_Factory::getInstance();
            $service = $serviceFactory->getAuthService();
            
            $result = $service->openIDAuth($openid_id, $openid_returnTo);
                
            if ($result) { // Perform DB Authentication
                $authResult = $service->authenticate($openid_identity, '');
                if ($authResult) {
                    $user = Zend_Auth::getInstance()->getIdentity();
                    $this->_redirect($user->getRoles()->homepage);
                } else {
                    $this->view->errorMsg = 
                        $this->_translate->translate('notRegistered'); 
                }
            } else {
                $this->view->errorMsg = 
                        $this->_translate->translate('openidAuthError');
            }
        } else {
            $this->view->errorMsg = $this->_translate->translate('openidError');
        }
        
        // On errors clear user identity if present
        $service->logout();
        
        $this->view->formLogin = $form;
        return $this->render('activelogin');
    }
    
    /**
     * Perform user logout
     * 
     * @return void
     */
    public function logoutAction()
    {
         // Disable both view and layout
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        
        $serviceFactory = Model_Factory::getInstance();
        $service = $serviceFactory->getAuthService();
        $service->logout();
        
        $this->_redirect('/index');
    }
}
